1922B - Forming Triangles - CodeForces Solution


combinatorics implementation math

Please click on ads to support us..

C++ Code:

# include <bits/stdc++.h>

using namespace std;

int main(){
  int t;
  cin>>t;
  while(t--){

     long long n;
    cin>>n;
 vector<long long> arr(n+1, 0);
  for(long long i=0; i<n; i++){
  long long freq;
  cin>>freq;
  arr[freq]++;
  }
    long long fact = 0 ;
    
 long long prev=0;
 for(long long i=0; i<=n; i++){
  if(arr[i]>=3){
   long long mid = (arr[i]*(arr[i]-1));
   mid*=(arr[i]-2);
   mid/=6;
   fact+=mid;
  }
  if(arr[i]>=2){
   long long mid = (arr[i]*(arr[i]-1));
   mid/=2;
   fact+=(mid*prev);
  }
  prev+=arr[i];
 }

 cout<<fact<<"\n";
  
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours